package br.com.cubedesk.telas;

import br.com.cubedesk.engine.Engine;
import br.com.cubedesk.interfaces.InterfaceCliente;
import br.com.cubedesk.repositorio.RepositorioCliente;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.DefaultFormatterFactory;

public class AdicionarCliente extends javax.swing.JFrame {

    public AdicionarCliente(int id) {
        initComponents();
        Engine.mascaraTelefone(telefone1);
        Engine.mascaraTelefone(telefone2);
        if (id == 0) {
            this.id.setText("...");
        } else {
            this.telefone1.setFormatterFactory(new DefaultFormatterFactory(null));
            this.telefone2.setFormatterFactory(new DefaultFormatterFactory(null));
            this.id.setText(String.valueOf(id));
            InterfaceCliente repo = new RepositorioCliente();
            String empresa_ref = repo.pegarClientePorId(id).get(0).getNomeFantaziaCliente();
            AdicionarCliente.nomeFantazia.setText(empresa_ref);
            String endereco_ref = repo.pegarClientePorId(id).get(0).getEnderecoCliente();
            this.endereco.setText(endereco_ref);
            String contato_ref = repo.pegarClientePorId(id).get(0).getContatoCliente();
            this.contato.setText(contato_ref);
            String telefone_ref = repo.pegarClientePorId(id).get(0).getTelefoneCliente1();
            this.telefone1.setText(telefone_ref);
            String telefone2_ref = repo.pegarClientePorId(id).get(0).getTelefoneCliente2();
            this.telefone2.setText(telefone2_ref);
            String email_ref = repo.pegarClientePorId(id).get(0).getEmailCliente();
            this.email.setText(email_ref);
            int status_ref = repo.pegarClientePorId(id).get(0).getStatusCliente();
            if (status_ref == 1) {
                this.ativo.setSelected(true);
            } else {
                this.ativo.setSelected(false);
            }
        }
        if (!this.id.getText().equals("...")) {
            this.salvar.setText("Alterar");
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nomeFantazia = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        contato = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        email = new javax.swing.JTextField();
        telefone2 = new javax.swing.JFormattedTextField();
        telefone1 = new javax.swing.JFormattedTextField();
        ativo = new javax.swing.JCheckBox();
        salvar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        endereco = new javax.swing.JTextArea();
        id = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Adicionar Cliente");

        jLabel1.setText("Nome Fantazia");

        nomeFantazia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                nomeFantaziaKeyTyped(evt);
            }
        });

        jLabel2.setText("Contato");

        contato.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                contatoKeyTyped(evt);
            }
        });

        jLabel3.setText("Telefone");

        jLabel4.setText("Telefone 2");

        jLabel5.setText("E-mail");

        email.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                emailKeyTyped(evt);
            }
        });

        ativo.setSelected(true);
        ativo.setText("Cliente ativo?");

        salvar.setText("Salvar");
        salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarActionPerformed(evt);
            }
        });

        jLabel6.setText("Endereco Completo");

        endereco.setColumns(20);
        endereco.setLineWrap(true);
        endereco.setRows(3);
        endereco.setWrapStyleWord(true);
        endereco.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                enderecoKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(endereco);

        id.setText("...");

        jLabel8.setText("ID");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(id)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(salvar))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(nomeFantazia, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(contato)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(telefone1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(telefone2))
                            .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(ativo, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nomeFantazia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(contato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(telefone2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(telefone1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ativo)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(salvar)
                    .addComponent(id)
                    .addComponent(jLabel8))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void salvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarActionPerformed
        if (this.id.getText().equals("...")) {
            String nomeFantazia_ref = AdicionarCliente.nomeFantazia.getText();
            String endereco_ref = this.endereco.getText();
            String contato_ref = this.contato.getText();
            String telefone_ref = this.telefone1.getText();
            String telefone2_ref = this.telefone2.getText();
            String email_ref = this.email.getText();
            int ativo_ref;
            if (this.ativo.isSelected()) {
                ativo_ref = 1;
            } else {
                ativo_ref = 2;
            }

            InterfaceCliente cliente = new RepositorioCliente();
            cliente.salvarCliente(nomeFantazia_ref, endereco_ref, contato_ref, telefone_ref,
                    telefone2_ref, email_ref, ativo_ref);
            this.dispose();
        } else {
            int id_ref = Integer.parseInt(this.id.getText());
            String nomeFantazia_ref = AdicionarCliente.nomeFantazia.getText();
            String endereco_ref = this.endereco.getText();
            String contato_ref = this.contato.getText();
            String telefone_ref = this.telefone1.getText();
            String telefone2_ref = this.telefone2.getText();
            String email_ref = this.email.getText();
            int ativo_ref;
            if (this.ativo.isSelected()) {
                ativo_ref = 1;
            } else {
                ativo_ref = 2;
            }
            InterfaceCliente cliente = new RepositorioCliente();
            cliente.alterarCliente(id_ref, nomeFantazia_ref, endereco_ref, contato_ref,
                    telefone_ref, telefone2_ref, email_ref, ativo_ref);
            this.dispose();
            InterfaceCliente c1 = new RepositorioCliente();
            DefaultTableModel model = (DefaultTableModel) BuscarClientes.tabela.getModel();
            model.setRowCount(0);
            this.repaint();
            int i = 0;
            for (Object ob : c1.pegarClientePorNome(nomeFantazia_ref)) {
                model.addRow(new Object[]{
                    c1.pegarClientePorNome(nomeFantazia_ref).get(i).getIdCliente(),
                    c1.pegarClientePorNome(nomeFantazia_ref).get(i).getNomeFantaziaCliente(),
                    c1.pegarClientePorNome(nomeFantazia_ref).get(i).getEnderecoCliente(),
                    c1.pegarClientePorNome(nomeFantazia_ref).get(i).getStatusCliente()
                });
                i++;
            }
        }

    }//GEN-LAST:event_salvarActionPerformed

    private void nomeFantaziaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nomeFantaziaKeyTyped
        char keyChar = evt.getKeyChar();
        if (Character.isLowerCase(keyChar)) {
            evt.setKeyChar(Character.toUpperCase(keyChar));
        }
    }//GEN-LAST:event_nomeFantaziaKeyTyped

    private void contatoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_contatoKeyTyped
        char keyChar = evt.getKeyChar();
        if (Character.isLowerCase(keyChar)) {
            evt.setKeyChar(Character.toUpperCase(keyChar));
        }
    }//GEN-LAST:event_contatoKeyTyped

    private void emailKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_emailKeyTyped
        char keyChar = evt.getKeyChar();
        if (Character.isLowerCase(keyChar)) {
            evt.setKeyChar(Character.toUpperCase(keyChar));
        }
    }//GEN-LAST:event_emailKeyTyped

    private void enderecoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_enderecoKeyTyped
        char keyChar = evt.getKeyChar();
        if (Character.isLowerCase(keyChar)) {
            evt.setKeyChar(Character.toUpperCase(keyChar));
        }
    }//GEN-LAST:event_enderecoKeyTyped

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AdicionarCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AdicionarCliente(0).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox ativo;
    private javax.swing.JTextField contato;
    private javax.swing.JTextField email;
    private javax.swing.JTextArea endereco;
    private javax.swing.JLabel id;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    public static javax.swing.JTextField nomeFantazia;
    private javax.swing.JButton salvar;
    private javax.swing.JFormattedTextField telefone1;
    private javax.swing.JFormattedTextField telefone2;
    // End of variables declaration//GEN-END:variables
}
